//
//  HopViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HopViewController.h"
#import "UILabel+HWDUILabel.h"
#import "EditCell.h"
#import "SelfTextView.h"
#import "RWBChouseView.h"
@interface HopViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,strong)NSArray *BaseTitleArr;//标题

@property (nonatomic, copy) NSString *desiredPosition;
@property (nonatomic, copy) NSString *jobType;
@property (nonatomic, copy) NSString *desiredCity;
@property (nonatomic, copy) NSString *desiredIncome;

@end

@implementation HopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"期望工作"];
    [self initRightBarButtonWithTitle:@"保存" andTitleColor:[UIColor whiteColor] andFont:16*screenW_6];
    _BaseTitleArr = @[@"期望职位",@"任职类型",@"期望城市",@"期望月薪"];
    
    [self.view addSubview:self.tableV];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _BaseTitleArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditCell"];
    if (!cell) {
        cell = [[EditCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditCell"];
    }
    NSString *str = @"请选择";
    if (indexPath.row == 0 || indexPath.row == 2 ) {
        str = @"请输入";
    }
    NSString *text;
    if (indexPath.row == 0) {
        text = _data[@"desiredPosition"];
    }else if (indexPath.row == 1){
        NSArray *arr = @[@"全职",@"兼职",@"实习"];
        text = arr[[_data[@"jobType"] integerValue]];
    }else if (indexPath.row == 2){
        text = _data[@"desiredCity"];
    }else{
        NSArray *arr = @[@"2k以下",@"2k-5k",@"5k-10k",@"10k-15k",@"15k-25k",@"25k-50k",@"50k以上"];
        text = arr[[_data[@"desiredIncome"] integerValue]];
    }
    
    [cell loadCell:_BaseTitleArr[indexPath.row] text:text placeHodel:[NSString stringWithFormat:@"%@%@",str,_BaseTitleArr[indexPath.row]]];
    if (indexPath.row == 1 || indexPath.row == 3) {
        cell.JianTouView.hidden = NO;
        cell.textF.inputTf.enabled = NO;
    }
    __weak typeof(self) weakSelf = self;
    cell.blcok = ^(NSString *text){
        if (indexPath.row == 0) {
            [weakSelf.data setObject:text forKey:@"desiredPosition"];
        }else if (indexPath.row == 2){
            [weakSelf.data setObject:text forKey:@"desiredCity"];
        }
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr;
    if (indexPath.row == 1) {
        arr = @[@"全职",@"兼职",@"实习"];
    }else if (indexPath.row == 3){
        arr = @[@"2k以下",@"2k-5k",@"5k-10k",@"10k-15k",@"15k-25k",@"25k-50k",@"50k以上"];
    }
    __weak typeof(self) weakSelf = self;
    RWBChouseView *view = [[RWBChouseView alloc]initWithTitles:arr];
    view.chouseTitleblock = ^(NSString *text){
        if (indexPath.row == 1) {
            [weakSelf.data setObject:[NSString stringWithFormat:@"%ld",[arr indexOfObject:text]] forKey:@"jobType"];
        }else if(indexPath.row == 3){
            [weakSelf.data setObject:[NSString stringWithFormat:@"%ld",[arr indexOfObject:text]] forKey:@"desiredIncome"];
        }
        [weakSelf.tableV reloadData];
    };
    [view showOnView:self.view];
}


-(void)onTapRightBarButton{//保存
    if ([NSString stringWithFormat:@"%@",_data[@"desiredPosition"]].length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入期望职位" timeInterval:2];
        return;
    }
    if ([NSString stringWithFormat:@"%@",_data[@"jobType"]].length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择任职类型" timeInterval:2];
        return;
    }
    if ([NSString stringWithFormat:@"%@",_data[@"desiredCity"]].length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入期望城市" timeInterval:2];
        return;
    }
    if ([NSString stringWithFormat:@"%@",_data[@"desiredIncome"]].length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请选择期望职月薪" timeInterval:2];
        return;
    }
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/updateResume" Dic:@{@"desiredPosition":_data[@"desiredPosition"],@"jobType":_data[@"jobType"],@"desiredCity":_data[@"desiredCity"],@"desiredIncome":_data[@"desiredIncome"],@"id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:@"保存成功" timeInterval:2 block:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                    if (_block) {
                        _block(_data);
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
